/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.commands;

import java.util.Stack;

/**
 * Uses command pattern.
 *
 * @author Rik Bauwens
 */
public class CommandStack
{
        private Stack<Command> undoStack;
        private Stack<Command> redoStack;

        public CommandStack()
        {
                undoStack = new Stack<Command>();
                redoStack = new Stack<Command>();
        }

        public void executeCommand(Command command)
        {
                command.execute();
                undoStack.add(command);
                redoStack.clear();
        }

        public boolean canUndo()
        {
                return !undoStack.isEmpty();
        }

        public boolean canRedo()
        {
                return !redoStack.isEmpty();
        }

        public void undo()
        {
                if(undoStack.isEmpty())
                        return;
                Command command = undoStack.pop();
                redoStack.push(command);
                command.undo();
        }

        public void redo()
        {
                if(redoStack.isEmpty())
                        return;
                Command command = redoStack.pop();
                undoStack.push(command);
                command.execute();
        }
}
